<?php
/**
 * Created by PhpStorm.
 * User: lixy
 * Date: 2016/12/6
 * Time: 17:43
 */

namespace app\librarys;

require_once "weixinpay/weixin_conf.php";

class WxTools
{
//获取微信jssdk conf参数
    public static function getJs_SignPackage()
    {
        $appId = WEIXIN_APPID;


        $access_token = self::get_weixin_token();
        $jsapiTicket = $access_token->get_jsapi_ticket->ticket;

        $url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
        $timestamp = time();
        $nonceStr = self::createNonceStr();

        // 这里参数的顺序要按照 key 值 ASCII 码升序排序
        $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";

        $signature = sha1($string);

        $signPackage = array(

            "nonceStr" => $nonceStr,
            "timestamp" => $timestamp,
            "url" => $url,
            "signature" => $signature,
            "appId" => WEIXIN_APPID,

        );
        return $signPackage;
    }

    public static function get_weixin_token_remote()
    {
     /**   $url = "http://wx.duolahe.com/biz_weixin/web/index.php/weixin/get-wx-token-handle";
        $re = Tools::postJson($url, null);
        return json_decode($re)->data;
**/
    
        $url = "http://wx.duolahe.com/biz_weixin/web/index.php/weixin/get-wx-token-handle";
        $re = Tools::postJson($url, null);
        $msg = null;
        try {
          $msg = json_decode($re)->data;
        } catch (\Exception $ex){
          \Yii::$app->cache->delete('wx_access_token_object');
          throw new \Exception('网络抽风了，请重新打开（刷新）页面吧。');
        }
        return $msg;
    }

    public static function get_weixin_token()
    {
       //\Yii::$app->cache->delete('wx_access_token_object');
        //echo 'debug';
        //exit;
        $key = 'wx_access_token_object';
        $now_time = time();
        $cache = \Yii::$app->cache;
        //判断本地的weixin_token是否存在
        $token_object = $cache->get($key);
        var_dump($token_object);
        //exit;
        if ($token_object === false) {
            $token_object = self::get_weixin_token_remote();
             
         //echo '11:';
         var_dump($token_object);
         //exit;

            $cache->set($key, $token_object);
            return $token_object;
        }
        // 因为有200秒的差值，所以这200秒会一直访问服务器，防止2个系统时间不一致和传输时间差）
        if ($now_time - $token_object->get_token_time > 7000 - 200) {
            $token_object = self::get_weixin_token_remote();
   
           // echo '22';
            var_dump($token_object);
            //exit;

            $cache->set($key, $token_object);
        }
   
            //echo '33';
            var_dump($token_object);
            //exit;
        return $token_object;
    }

    public static function createNonceStr($length = 16)
    {
        $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        $str = "";
        for ($i = 0; $i < $length; $i++) {
            $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }
        return $str;
    }
}
